//
//  BabyBirthdayView.m
//  BabyHelper
//
//  Created by  thomas on 11-2-18.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BabyBirthdayView.h"


@implementation BabyBirthdayView
@synthesize datePicker;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	self.title = NSLocalizedString(@"Set Birthday", @"");
    [super viewDidLoad];
	
	UIBarButtonItem *saveItem = [[UIBarButtonItem alloc]
								 initWithTitle:NSLocalizedString(@"Done",@"")
								 style:UIBarButtonItemStyleBordered 
								 target:self
								 action:@selector(toggleSave)		
								 ];
	self.navigationItem.rightBarButtonItem = saveItem;
	[saveItem release];	
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.datePicker = nil;
}


- (void)dealloc {
    [super dealloc]; 
}

-(void)viewWillAppear:(BOOL)animated{
	
	Dao *dao = [Dao getInstance];
	BabyProfile *babyProfile = [[BabyProfile alloc]init];
	[dao LoadBabyProfile:babyProfile];
	
	if( babyProfile.Birthday == 0 )
		[self.datePicker setDate:[NSDate date]];
	else
		[self.datePicker setDate:[NSDate dateWithTimeIntervalSince1970:babyProfile.Birthday]];
	
	[babyProfile release];
}

-(void)toggleSave{
	NSUserDefaults *prefs   = [NSUserDefaults standardUserDefaults];
	double timeValue        = (double)[[self.datePicker date] timeIntervalSince1970];
	[prefs setDouble:timeValue forKey:@"Birthday"]; 
	[self.navigationController popViewControllerAnimated:true];		
}
@end
